{"componentChunkName":"component---src-templates-post-jsx","path":"/[Algorithm]이분 탐색/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"07844ce0-f154-5ad8-b4cc-c6e9bebecc21","excerpt":"🧷 이분 탐색 탐색 범위를 두 부분으로 분할하면서 찾는 방식 처음부터 끝까지 탐색하는 것보다 더 빠른 시간 복잡도를 가지는 방식 🪚 과정 가장 핵심은 정렬이 되어 있어야 한다는 점이다.  와  를 사용해  를 설정한다.  와 구하고자 하는 값을 비교한다. 구하고자 하는 값이 더 큰 경우,  더 작은 경우,   가 될 때까지 반복 탐색할 배열의 범위를 벗어…","html":"<h2>🧷 이분 탐색</h2>\n<p>탐색 범위를 두 부분으로 분할하면서 찾는 방식</p>\n<ul>\n<li>처음부터 끝까지 탐색하는 것보다 더 빠른 시간 복잡도를 가지는 방식</li>\n</ul>\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">**시간 복잡도**\n전체 탐색: O(N)\n이분 탐색: O(logN)</code>\n        </deckgo-highlight-code>\n<h3>🪚 과정</h3>\n<p>가장 핵심은 <strong>정렬이 되어 있어야 한다</strong>는 점이다.</p>\n<ul>\n<li><code class=\"language-text\">left</code> 와 <code class=\"language-text\">right</code> 를 사용해 <code class=\"language-text\">mid</code> 를 설정한다.</li>\n<li>\n<p><code class=\"language-text\">mid</code> 와 구하고자 하는 값을 비교한다.</p>\n<ul>\n<li>구하고자 하는 값이 더 큰 경우, <code class=\"language-text\">left = mid + 1</code></li>\n<li>더 작은 경우, <code class=\"language-text\">right = mid - 1</code></li>\n</ul>\n</li>\n<li><code class=\"language-text\">left > right</code> 가 될 때까지 반복</li>\n<li>탐색할 배열의 범위를 벗어난다면, 구하고자 하는 값이 배열에 없음을 판단할 수 있다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">int binarySearch(int[] numbers, int M) {\n  Arrays.sort(numbers);\n\n  int left = 0;\n  int right = numbers.length - 1;\n  int mid; \n\n  while (left &lt; right) {\n    mid = (left + right) / 2;\n\n    if (numbers[mid] == M)\n      return mid;\n    else if (numbers[mid] &lt; M) \n      left = mid + 1;\n    else \n      right = mid - 1;\n  }\n}</code>\n        </deckgo-highlight-code>\n<h3>🪚 추가 예제</h3>\n<p>이분 탐색은 정렬된 데이터에서 특정 값을 찾고자 할 때 빠른 시간 복잡도로 탐색이 가능하다는 장점이 있다. 이를 이용해 정수 N이 주어졌을 때, N의 제곱근을 구하는 코드를 작성해본다.</p>\n<ul>\n<li>1부터 주어진 정수까지의 단순 반복 및 확인으로 구할 수는 있지만 이는 <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"mclose\">)</span></span></span></span>의 시간 복잡도를 가진다. </li>\n</ul>\n<p>이를 이분 탐색을 사용하여 \"0부터 N까지의 자연수 중 N의 제곱근이 있는지 없는지\" 확인하는 코드로 변경한다. 즉, <strong>0부터 N까지의 정렬된 자연수 중에서 특정 값을 찾고 있는 것</strong>이다.</p>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">int squareRootSearch(int M) {\n  int left = 0;\n  int right = M;\n  int num, squareNum; \n\n  while (left &lt; right) {\n    num = (left + right) / 2;\n    squareNum = num * num;\n\n    if (squareNum == M)\n      return num;\n    else if (squareNum &lt; M) \n      left = num + 1;\n    else \n      right = num - 1;\n  }\n}</code>\n        </deckgo-highlight-code>\n<hr>\n<h2>📕 참고</h2>\n<ul>\n<li><a href=\"https://gyoogle.dev/blog/algorithm/Bubble%20Sort.html\">Tech Interview for developer</a></li>\n<li><a href=\"https://cjh5414.github.io/binary-search/\">이진 탐색(Binary Search) 알고리즘 개념 이해 및 추가 예제</a></li>\n</ul>","frontmatter":{"title":"💡 이분 탐색(Binary Search)","date":"May 05, 2022","update":"May 05, 2022","tags":["개발상식","Java","이분탐색"],"series":"💡 Algorithm"},"fields":{"slug":"/[Algorithm]이분 탐색/","readingTime":{"minutes":2.655}}},"seriesList":{"edges":[{"node":{"id":"b0e662c4-8dab-58b1-8e34-76fd1f26ad52","fields":{"slug":"/[Algorithm]graph/"},"frontmatter":{"title":"💡 Graph 이론"}}},{"node":{"id":"c1c6944f-734a-5276-8604-a897181207ae","fields":{"slug":"/[Algorithm]문제해결개관/"},"frontmatter":{"title":"💡 문제해결개관"}}},{"node":{"id":"b783c57f-f958-5c14-9935-06b16995e8f1","fields":{"slug":"/[Algorithm]알고리즘분석/"},"frontmatter":{"title":"💡 알고리즘 분석"}}},{"node":{"id":"f3c7f32a-2ead-5bd5-a198-ee5342823217","fields":{"slug":"/[Algorithm]Sorting 알고리즘/"},"frontmatter":{"title":"💡 Sorting 알고리즘"}}},{"node":{"id":"07844ce0-f154-5ad8-b4cc-c6e9bebecc21","fields":{"slug":"/[Algorithm]이분 탐색/"},"frontmatter":{"title":"💡 이분 탐색(Binary Search)"}}},{"node":{"id":"66ab5e60-ccc9-5f85-ad77-8457be4014db","fields":{"slug":"/[Algorithm]HashTable/"},"frontmatter":{"title":"💡 해시 테이블 (Hash Table)"}}},{"node":{"id":"8aa61523-9a0a-5cc7-a670-9750d851b450","fields":{"slug":"/[Algorithm]최장증가수열(LIS)/"},"frontmatter":{"title":"💡 최장 증가 수열(LIS)"}}},{"node":{"id":"db216133-c9cd-5948-babf-f437eb127d7c","fields":{"slug":"/[Algorithm]DynamicProgramming/"},"frontmatter":{"title":"💡 DP (Dynamic Programming)"}}},{"node":{"id":"fa96dd6e-75c9-5a7c-887c-1993125cc7f0","fields":{"slug":"/[Algorithm]최소공통조상(LCA)/"},"frontmatter":{"title":"💡 최소 공통 조상(LCA)"}}},{"node":{"id":"268a7d15-795c-5e9f-9770-eeda9132721d","fields":{"slug":"/[Algorithm]다익스트라알고리즘/"},"frontmatter":{"title":"💡 다익스트라(Dijkstra) 알고리즘"}}},{"node":{"id":"f3a9bade-22f4-54b0-ad9a-b642d6f2b754","fields":{"slug":"/[Algorithm]비트마스크/"},"frontmatter":{"title":"💡 비트마스크(BitMask)"}}}]},"previous":{"fields":{"slug":"/[Programmers]81303/"},"frontmatter":{"title":"👩‍💻 81303. 표 편집"}},"next":{"fields":{"slug":"/[Algorithm]HashTable/"},"frontmatter":{"title":"💡 해시 테이블 (Hash Table)"}}},"pageContext":{"id":"07844ce0-f154-5ad8-b4cc-c6e9bebecc21","series":"💡 Algorithm","previousPostId":"d9074b34-ffc6-5b1f-8668-55f64eb5da9f","nextPostId":"66ab5e60-ccc9-5f85-ad77-8457be4014db"}},"staticQueryHashes":[]}